<?php
namespace App\Common\Cla\Com\Upload\Module;

use App\Common as com;

class Picture extends com\Cla\ClaBase {

	/**
	 * @var \think\file\UploadedFile
	 */
	protected $fileInfo = null;
	
	/**
	 * @param File $file_info 文件类
	 */
	public function __construct( \think\file\UploadedFile $file_info ){
		$this->fileInfo = $file_info;
	}
	
	/**
	 * 模块处理器
	 * @param array $param 参数数组 <br />
	 * ················· [可空/多选_A/必须](unknown_type)	$param['AAAAA']	说明：AAAAA	<br />
	 * @param array &$return_data [传参]返回数组 <br />
	 * ················· [必须](int)		$return_data['status_str']	说明：返回状态说明	<br />
	 * ················· [必须](array)	$return_data['other_data']	说明：额外数据(api返回时使用)	<br />
	 * ················· [可空/多选_A/必须](unknown_type)	$return_data['AAAAA']	说明：AAAAA	<br />
	 * @return int 处理结果。[0]失败 [1]完成
	 */
	public function handle( array $param = [], array &$return_data = [] ){
		// ...
		$param['config_param'] = $param['config_param'] ?? [];
		if( empty( $param['config_param'] ) ) return 0;
		//
		$sub_data = [];
		$sub_data['param'] = $param;
		$sub_data['return_data'] = &$return_data;
		$sub_data['fileanme_path'] = $this->fileInfo->getPath() . DIRECTORY_SEPARATOR . $this->fileInfo->getFilename();
		$sub_data['module_data'] = $sub_data['param']['config_param']['sys']['module_data'] ?? [];
		// 处理图片分辨率
		$sub_status = $this->handleSubPicture( $sub_data );
		if( $sub_status !== 1 ) return $sub_status;
		//
		return 1;
	}
	
	/**
	 * 模块处理器->图片处理
	 * @param array $sub_data [传址]子程序数据
	 * @return int 处理结果 [0]处理失败 [1]处理成功
	 */
	protected function handleSubPicture( &$sub_data ){
		$image = \think\Image::open( $sub_data['fileanme_path'] );
		$is_deal_picture = 0;
		// 处理图片分辨率
		$sub_data['module_data']['max_width'] = $sub_data['module_data']['max_width'] ?? 0;
		if( $sub_data['module_data']['max_width'] > 0 ){
			if( $image->width() > $sub_data['module_data']['max_width']  || $image->height() > $sub_data['module_data']['max_height'] ){
				// 获取新的大小
				$new_info = $this->getNewSize( $image->width(), $image->height(), $sub_data['module_data']['max_width'], $sub_data['module_data']['max_height'] );
				// 开启缩放
				if( $new_info['zoom_ratio'] != 1 ){
					$image = $image->thumb( $new_info['width'], $new_info['height'] );
					//
					$is_deal_picture = 1;
				}
			}
			
		}
		// 压缩图片(计算转换图片质量)
		$filesize = com\Lib\Fun\File::getFileSize( $sub_data['fileanme_path'] );
		$sub_data['module_data']['max_filesize'] = $sub_data['module_data']['max_filesize'] ?? 0;
		if( $sub_data['module_data']['max_filesize'] > 0 && $filesize > $sub_data['module_data']['max_filesize'] ){
			$sub_data['module_data']['control_filesize'] = $sub_data['module_data']['control_filesize'] ?? $sub_data['module_data']['max_filesize'];
			// 
			$image_quality = round( $sub_data['module_data']['control_filesize'] / $filesize * 100, 0 );
			if( $image_quality < 20 ) $image_quality = 20; // 最低为20
			if( $image_quality > 100 ) $image_quality = 100; // 最高为100
			//
			$is_deal_picture = 1;
		}else{
			$image_quality = 85;
		}
		// 获取文件后缀
		$extension = strtolower( $this->fileInfo->extension() );
		if( $extension == 'png' ) $image_quality = 100 - $image_quality; // png 压缩比反转
		// 保存图片
		if( $is_deal_picture == 1 ){
			$data = $image->save( $sub_data['fileanme_path'], null, $image_quality ?? 80 );
		}
		// 检查图片大小
		if( $sub_data['module_data']['max_filesize'] > 0 && $sub_data['module_data']['max_filesize'] > 0 ){
			if( com\Lib\Fun\File::getFileSize( $sub_data['fileanme_path'] ) > $sub_data['module_data']['max_filesize'] ){
				$size_data = com\Lib\Fun\File::formatBytesInfo( $sub_data['module_data']['max_filesize'] );
				$sub_data['return_data']['status_str'] = "图片不可超过 {$size_data['size']}{$size_data['units']}，";
				//
				return 0;
			}
		}
		//
		return 1;
	}

	/**
	 * 模块处理器->图片处理
	 * @param array $sub_data [传址]子程序数据
	 * @return int 处理结果 [0]处理失败 [1]处理成功
	 */
	protected function DEL_handleSubMoveFile( &$sub_data){
		// 检查是否为oss
		
		// [oss]上传到oss
		
		// [oss]无论是否上传成功，都删除本地文件
	}
	
	/**
	 * 计算文件新的大小（等比缩放）
	 * @param int $old_width
	 * @param int $old_height
	 * @param int $max_width
	 * @param int $max_height
	 * @return array [
	 * 		'zoom_ratio' => 0, // 缩放比例
	 * 		'width' => 0, // 新宽度
	 * 		'height' => 0, // 新高度
	 * ]
	 */
	protected function getNewSize( $old_width, $old_height, $max_width, $max_height ){
		$new_info = [];
		//
		if( $old_width > $max_width ){
			$zoom_ratio = $max_width / $old_width;
		}else if( $old_height > $max_height ){
			$zoom_ratio = $max_height / $old_height;
		}
		//
		$new_info['zoom_ratio'] = $zoom_ratio;
		$new_info['width'] = $old_width * $zoom_ratio;
		$new_info['height'] = $old_height * $zoom_ratio;
		//
		return $new_info;
	}

	
}
